﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace IssueServiceLibrary
{
    [XmlRoot("ServiceUsers")]
    public class UserList
    {
        public const string UserFileName = "users.xml";
        List<User> usrs;

        public List<User> Users
        {
            get { return usrs; }
            set { usrs = value; }
        }

        /// <summary>
        /// default constructor for serialization
        /// </summary>
        public UserList()
        {
            usrs = new List<User>();
        }

        public static UserList ReadUsersSerializable()
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(UserList));

            if (File.Exists(UserFileName))
            {
                using (FileStream fs = File.OpenRead(UserFileName))
                {
                    UserList usList = (UserList)dcs.ReadObject(fs);
                    return usList;
                }
            }
            else
                return new UserList();
        }

        public static void WriterUsersSerializable(UserList usList)
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(UserList));
            using (FileStream fs = new FileStream(UserFileName, FileMode.Create))
            {
                dcs.WriteObject(fs, usList);
            }
        }
    }    
}
